/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Dec 12, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.data;

import edu.psu.geovista.app.gmatrix.JDfMatrix;
import edu.psu.geovista.app.gmatrix.event.MatrixSelectionListener;
import edu.psu.geovista.app.gmatrix.toolkit.FireEventSupport;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

public class DfSelectionModel {
    private static final Logger logger= Logger.getLogger(DfSelectionModel.class.getName());
    Set selection=new HashSet();

    int rowCount; //num of rows in the matrix, it is for calculate default full selection
    int colCount;

    private FireEventSupport evtfirer;
    public Set getSelection() {
        return selection;
    }
    public void addSelection(Set select){
        this.selection.addAll(select);
        this.getEvtfirer().fireSelectionChanged(selection);
    }
    public void removeSelection(Set select){
        if(selection!=null &&selection.size() >0) {
        this.selection.removeAll(select);
        this.getEvtfirer().fireSelectionChanged(selection);
        }
        else{
            //no selection yet, thus full selection
            Set<Point> fullSelection = this.getFullSelection();
            fullSelection.removeAll(select);
            this.selection =fullSelection;
             this.getEvtfirer().fireSelectionChanged(selection);
        }
    }

    public void setSelection(Set select) {

        this.selection.clear();
        this.selection.addAll(select);
         this.getEvtfirer().fireSelectionChanged(selection);
        //fireSelectionChange(selection);

    }

    /**
     * this is only for fire internal selection.
     * @param rows
     */
   /* public  void setSelectRows(int[] rows){
       this.getEvtfirer().fireSelectRowChanged(rows, MatrixSelectionEvent.SELECT_ROW);
    }
    public void setSelectCols(int[] cols){
         this.getEvtfirer().fireSelectColumnChanged(cols);
    }*/
    public void addMatrixSelectionChangeListener(MatrixSelectionListener l){
        this.getEvtfirer().addListener(MatrixSelectionListener.class, l);
    }

    public FireEventSupport getEvtfirer() {
        if(evtfirer==null){
            evtfirer=new FireEventSupport(this);
        }
        return evtfirer;
    }

    public Set <Point> getFullSelection(){

        int rowCount = this.getRowCount();
        int colCount = this.getColCount();
        return JDfMatrix.getFullSelection(rowCount, colCount);
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getColCount() {
        return colCount;
    }

    public void setColCount(int colCount) {
        this.colCount = colCount;
    }
}
